package com.example.travelgd.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.util.ClassUtils;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 开发环境配置类
 * 用于配置静态资源访问路径和文件上传路径
 */
@Configuration
@Profile("dev")
public class MyDevConfig implements WebMvcConfigurer {

    @Value("${file.upload-path}")
    private String uploadPath;

    @Bean
    public String imagePath() {
        return ClassUtils.getDefaultClassLoader().getResource("").getPath() + "static/images";
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String classPath = ClassUtils.getDefaultClassLoader().getResource("").getPath();
        String imagePath = classPath + "static/images/";

        // 新闻图片
        registry.addResourceHandler("/static/news/**")
                .addResourceLocations("classpath:/static/news/")
                .setCachePeriod(3600);

        // 景区图片
        registry.addResourceHandler("/images/scenic/**")
                .addResourceLocations("file:" + imagePath + "scenic/");

        // 美食图片
        registry.addResourceHandler("/images/food/**")
                .addResourceLocations("file:" + imagePath + "food/");

        // 特产图片
        registry.addResourceHandler("/images/specialty/**")
                .addResourceLocations("file:" + imagePath + "specialty/");

        // 其他静态资源 - 保持原有的/static/**匹配
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/")
                .setCachePeriod(3600);

        // 文件上传路径
        registry.addResourceHandler("/files/**")
                .addResourceLocations("file:" + uploadPath + "/");
    }
}